package code.structure.module.scene.manager
{
	import code.main.AppData;
	import code.structure.manager.loader.DllLoaderManager;
	import code.structure.manager.mouse.MouseManager;
	import code.structure.module.configs.data.AreaConfigData;
	import code.structure.module.configs.data.ItemConfigData;
	import code.structure.module.scene.SceneController;
	import code.structure.module.scene.model.SceneModel;
	import code.structure.module.scene.view.YIsoWorld;
	import code.structure.module.scene.view.iso.YIsoObject;
	import code.structure.module.scene.view.iso.product.ProductPloughIsoObject;
	import code.structure.module.scene.view.iso.product.base.ProductIsoObject;
	import code.untils.CommandNameConst;
	import code.untils.IsoStatusConst;
	import code.untils.SceneConfig;
	import code.untils.TriggerConst;
	
	import com.patience.map.isometric.area.AreaMap;
	import com.patience.map.isometric.utils.IsoUtils;
	import com.patience.mvc.context.Command;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	/**
	 *正常状态下 场景管理器
	 * @author Yuri
	 *
	 */
	public class NormalSceneManager
	{
		private var _isoWorld:YIsoWorld;
		private var _sceneModel:SceneModel;
		private var _controller:SceneController;
		
		private var _normalType:String;
		
		/**
		 *当前种植种子数据
		 */
		private var _currentPlantSeedData:ItemConfigData;
		/**
		 *当前种植的icon图标
		 */
		private var _currentPlantIcon:Sprite;
		
		/**
		 *命令变量
		 */
		private var _cmd:Command = new Command("");
		
	/*	private var _bindIsoTip:BaseTips;
		private var _lockLandTips:LockedLandTips;
		private var _shopLockLandTips:ShopLockedLandTips;*/
		/**
		 *进度条tip集合
		 */
		private var _progressBarTipsList:Array;
		
		/**
		 *当前被指引的场景对象
		 */
		private var _currentGuideIsoObject:YIsoObject;
		
		/**
		 *当前被指引的场景区域 
		 */
		private var _currentGuideArea:AreaMap;
		
		/**
		 *当前被点击需要种植的耕地 
		 */
		private var _currentNeedPlantPlot:ProductPloughIsoObject;
		/**
		 *区域指引tip （直接存放在场景中的） 
		 */
//		private var _areaGuideTip:GoTips;
		
		public function NormalSceneManager(iWorld:YIsoWorld = null)
		{
			_isoWorld = iWorld;
			init();
		}
		
		private function init():void
		{
			_normalType = SceneConfig.SCENE_NORMA_NORMAL;
			_currentPlantIcon = null;
			_currentPlantSeedData = null;
		/*	_bindIsoTip = null;
			_lockLandTips = null;
			_shopLockLandTips = null;*/
			_currentGuideIsoObject = null;
			_currentGuideArea = null;
			_currentNeedPlantPlot = null;
			_progressBarTipsList = [];
		}
		
		/**
		 * 世界中鼠标移动
		 *
		 */
		public function mouseMoveIsoWorld(stageX:Number, stageY:Number):void
		{
			if (normalType == SceneConfig.SCENE_NORMA_PLANT && _currentPlantIcon)
			{
				var p:Vector3D = _isoWorld.getIsoWorldPoint3DByScene(stageX, stageY);
				var $point:Point = IsoUtils.isoToScreen(p);
				_currentPlantIcon.x = $point.x/* - _currentPlantIcon.width/2*/;
				_currentPlantIcon.y = $point.y/* - _currentPlantIcon.height/2*/;
			}
		}
		
		/**
		 *鼠标移到一个Iso对象
		 * @param iso
		 *
		 */
		public function mouseOverIsoObject(iso:YIsoObject, stageX:Number, stageY:Number):void
		{
				iso.status = IsoStatusConst.ISO_STATUS_OVER;
		}
		
		/**
		 *鼠标移出一个Iso对象
		 * @param iso
		 *
		 */
		public function mouseOutIsoObject(iso:YIsoObject):void
		{
			iso.status = IsoStatusConst.ISO_STATUS_NORMAL;
		}
		
		/**
		 * 世界中空地点击
		 *
		 */
		public function mouseSpaceClick(stageX:Number, stageY:Number):void
		{
			if (_sceneModel.isMyScene)
			{
				var p:Point = _isoWorld.getGridPoint3D(stageX, stageY);
				if (!mouseClickLockArea(p.x, p.y))
					return;
				
				if (normalType == SceneConfig.SCENE_NORMA_PLANT)
				{
					canclePlantSeed();
				}
			}
		}
		
		/**
		 *选择点击的对象
		 * @param iso
		 *
		 */
		public function mouseClickIsoObject(iso:YIsoObject, stageX:Number, stageY:Number):void
		{
			clearCommand();
			
			checkSelectGuideIso(iso); //检测是否引导选中 并清除 
			
			//自己场景 并且 改物件是未解锁的
			if (!iso.isUnlock)
			{
				mouseClickLockArea(iso.gridX, iso.gridZ); //============================================ CYang
				return;
			}
			
			/*else
			{
				selectSpaceClick(stageX, stageY);
			}*/
		}
		
		/**
		 *选中生产型对象 
		 * @param pIso
		 * 
		 */
		public function selectedProductIsoObject(pIso:ProductIsoObject):void{
		}
		
		/**
		 *检测是否选中指引场景元件
		 * @param iso
		 *
		 */
		private function checkSelectGuideIso(iso:YIsoObject):void
		{
			if(!iso) return;
			if (_currentGuideIsoObject && _currentGuideIsoObject == iso)
			{
				_currentGuideIsoObject.isGuide = false;
				_currentGuideIsoObject = null;
			}
		}
		
		/***********************************************场景对象本身*********************************************/
		
		/**
		 * 场景元件升级
		 * @param iso
		 *
		 */
		public function upgrade(iso:YIsoObject):void
		{
			/*_sceneModel.levelUpBuilding(iso);*/
		}
		
		
		/***********************************************自己家场景对对象的操作*********************************************/
		
		/**
		 *指引场景对象
		 * @param iso
		 *
		 */
		public function guideIsoObject(iso:YIsoObject):void
		{
			//关闭其他模块指引
			clearCommand();
			_cmd.cmdName = CommandNameConst.COMMAND_CLOSE_GO_TIPS_BY_FRIENDS;
			if(controller) controller.dispatch(_cmd);
			if (_currentGuideIsoObject)
			{
				if (_currentGuideIsoObject != iso){
					_currentGuideIsoObject.isGuide = false;
					_currentGuideIsoObject = null;
				}
				else
					return;
			}
			if(AppData.isNew) return;
			_currentGuideIsoObject = iso;
			_currentGuideIsoObject.isGuide = true;
		}
		
		/**
		 *指引区域 
		 * @param areaKey
		 * 
		 */
		public function guideArea(areaKey:String):void{
			/*if(!AppData.isMyScene) return;
			clearGuideArea();
			if(_isoWorld && _isoWorld.mapInfo.areaInfo &&  _isoWorld.mapInfo.areaInfo.areaMapDic){
				_currentGuideArea = _isoWorld.mapInfo.areaInfo.areaMapDic[areaKey] as AreaMap;
				if(!_currentGuideArea) return;
				var p:Point =_currentGuideArea.centerGridPoint;
				_isoWorld.moveContentXYtoCenterByGridPoint(p);
				if(!_areaGuideTip)_areaGuideTip = new GoTips();
				var pp:Vector3D = _isoWorld.getIsoWorldPoint3DByGridPosition(p);
				var $point:Point = IsoUtils.isoToScreen(pp);
				_isoWorld.sceneTipContainer.addChild(_areaGuideTip);*/
//				_areaGuideTip.show($point.x,$point.y+_isoWorld.sceneTipContainer.height-110,'newHandArrow');
//			}
		}
		
		/**
		 *清除区域指引 
		 * 
		 */
		private function clearGuideArea():void{
			/*if(_areaGuideTip && _isoWorld.sceneTipContainer.contains(_areaGuideTip)){
				_isoWorld.sceneTipContainer.removeChild(_areaGuideTip);
			}
			_areaGuideTip = null;
			_currentGuideArea = null;*/
		}
		
		
		/**
		 *清除种子种植,相当于清空耕地信息 
		 * @param iso
		 * 
		 */
		private function clearSeedProductProgress(iso:ProductPloughIsoObject):void{
			if(!iso || !iso.productShopItemData) return;
			if(_sceneModel.clearSeed(iso)){
				iso.cancelProduct();
			}
		}
		
		/**
		 *选中种植的种子
		 * @param d
		 *
		 */
		public function selectedPlantSeed(d:ItemConfigData):void
		{
			canclePlantSeed(false);
			if (!d)
			{
				return;
			}
			_currentPlantSeedData = d;
			normalType = SceneConfig.SCENE_NORMA_PLANT;
			//如果有之前选择的空格耕地
			if(_currentNeedPlantPlot){
				if(checkPlantSeed(_currentNeedPlantPlot)){
					createPlantSeedIcon();
				}
				_currentNeedPlantPlot = null;
			}else{
				createPlantSeedIcon();
			}
		}
		
		/**
		 * 处理种子植物物件检测
		 * @param iso
		 * @return true可以继续种植 false停止种植样式
		 * 
		 */
		private function checkPlantSeed(iso:ProductPloughIsoObject):Boolean
		{
			if (!iso)
				return false;
			if (!iso || iso.productShopItemData)
				canclePlantSeed();
			if (_currentPlantSeedData)
			{
				/*(iso as ProductPloughIsoObject).prepareSeedData = _currentPlantSeedData;
				addNpcTaskQueue(iso);
				if(AppData.isNew || !_isoWorld.infoManager.checkIfHaveCanSeedPlough()){
					canclePlantSeed();
					return false;
				}*/
				return true;
			}
			return false;
		}
		
		/**
		 *创建种植Icon
		 *
		 */
		private function createPlantSeedIcon():void
		{
			_currentPlantIcon = new (DllLoaderManager.getInstance().getMaterial(_currentPlantSeedData.sPath, "ICON") as Class) as Sprite;
			_isoWorld.sceneTipContainer.addChild(_currentPlantIcon);
			_currentPlantIcon.mouseEnabled = _currentPlantIcon.mouseChildren = false;
		}
		
		/**
		 * *取消播种操作
		 * @param isClear true的时候表示所有的关于播种的行为都取消 false只是取消图标
		 * 
		 */
		private function canclePlantSeed(isClear:Boolean=true):void
		{
			if (_currentPlantIcon && _currentPlantIcon.parent)
			{
				_isoWorld.sceneTipContainer.removeChild(_currentPlantIcon);
				_currentPlantIcon = null;
			}
			if(normalType!=SceneConfig.SCENE_NORMA_PLANT) return; //当目前模式不是种植模式时候 下面不操作
			if(isClear){
				_currentPlantSeedData = null;
				_currentNeedPlantPlot = null;
				normalType = SceneConfig.SCENE_NORMA_NORMAL;
				if(controller) controller.dispatch(new Command(CommandNameConst.COMMAND_CANCLE_PLANT_BYSCENE)); //抛出取消种植通知
			}
		}
		
		/**
		 *退出种植模式 
		 * 
		 */
		public function exitPlant():void{
			canclePlantSeed();
		}
		
	
		
		/**
		 *播放进度条
		 * @param iso
		 * @param playTime
		 *
		 */
		public function playProgressBar(iso:YIsoObject, playTime:Number = 2):void
		{
			/*var progressBar:ProgressBarTips = new ProgressBarTips(_isoWorld.sceneTipContainer, BaseTips.TIPS_LAYER_TYPE_HOVER_SCENE, progressplayComplete);
			progressBar.show(iso, playTime);
			_progressBarTipsList.push(progressBar);*/
		}
		
		/********************************************************************************************************************/
		
		public function get controller():SceneController
		{
			return _controller;
		}
		
		public function set controller(value:SceneController):void
		{
			_controller = value;
			if (value)
				_sceneModel = _controller.sceneModel;
		}
		
		/**
		 *正常状态下的类型
		 */
		public function get normalType():String
		{
			return _normalType;
		}
		
		/**
		 * @private
		 */
		public function set normalType(value:String):void
		{
			_normalType = value;
		}
		
		/**
		 *退出
		 *
		 */
		public function exit():void
		{
			//			canclePlantSeed();
			exitPlant();
			clearCommand();
			clearSceneTip();
			clearGuideArea();
			MouseManager.getInstance().clearFollowCursor();
			outArea();
		}
		
		/**
		 *清除场景鼠标移上去的Tip
		 *
		 */
		private function clearSceneTip():void
		{
			clearCurrentTargetFocus();
			if(!_progressBarTipsList) return;
			var count:int = _progressBarTipsList.length;
			for (var i:int = 0; i < count; i++)
			{
				if(_progressBarTipsList[i]) 
					_progressBarTipsList[i].close();
			}
			_progressBarTipsList.length = 0;
		}
		
		/**
		 *清除当前对象的tip 
		 * @param isClearIso
		 * @param isClearArea
		 * 
		 */
		public function clearCurrentTargetFocus(isClearIso:Boolean=true,isClearArea:Boolean=true):void{
			/*if(isClearIso){
				if (_bindIsoTip && _bindIsoTip.parent)
				{
					_bindIsoTip.close();
				}
				_bindIsoTip = null;
			}
			if(isClearArea){
				outArea();
				if (_lockLandTips && _lockLandTips.parent)
				{
					_lockLandTips.close();
				}
				_lockLandTips = null;
				if (_shopLockLandTips && _shopLockLandTips.parent)
				{
					_shopLockLandTips.close();
				}
				_shopLockLandTips = null;
			}*/
		}
		
		/**
		 *清除Command
		 *
		 */
		private function clearCommand():void
		{
			if (!_cmd)
				return;
			_cmd.cmdName = "";
			_cmd.target = null;
			_cmd.data = null;
		}
		
		public function clear():void
		{
			exit();
			init();
		}
		
		/**************************************** 引导箭头状态 CYang*******************************************************************/ /**
		* 设置引导是否可见
		* @param	sceneState 当前场景状态
		*/
		public function setGuideState(sceneState:Boolean):void
		{
			if (!_currentGuideIsoObject)
				return;
			_currentGuideIsoObject.isGuide = sceneState;
			_currentGuideIsoObject = null;
		}
		
		/**************************************** 解锁地图区块 CYang*******************************************************************/
		
		private var _m_key:String;
		private var lockData:AreaConfigData;
		private var areMap:AreaMap;
		
		/**
		 *  鼠标点击到锁定区块
		 * @param x
		 * @param z
		 * @return  true  已解锁区域 false锁定区域
		 * 
		 */
		public function mouseClickLockArea(x:int, z:int):Boolean
		{
			clearGuideArea();
			var b:Boolean = _isoWorld.mapInfo.areaInfo.checkAreaIsUnLocked(x, z);
			if (b)
			{
				outArea();
				return true;
			}
			_m_key = null;
			var data:AreaConfigData = getLockAreaData(x, z, false);
			/*if (data){
				_isoWorld.sceneAlertManger.showEnlargeLandAlert(data, _isoWorld.infoManager.unLockAreaScene);
				
			}*/
			return false;
		}
		
		/**
		 * 鼠标移入锁定区块
		 */
		public function mouseMoveLockArea(grid:Point):void
		{
		/*	var b:Boolean = _isoWorld.mapInfo.areaInfo.checkAreaIsUnLocked(grid.x, grid.y);
			if (b)
			{
				if (_bindIsoTip)
					_bindIsoTip.close();
				if (_lockLandTips)
					_lockLandTips.close();
				if (_shopLockLandTips)
					_shopLockLandTips.close();
				outArea();
				return;
			}
			var data:AreaConfigData = getLockAreaData(grid.x, grid.y);
			if (data == null)
				return;
			//数据不为空显示Tip
			if(SceneConfig.SHOP_AREA_INDEX.indexOf(data.id) == -1)
			{
				if (_lockLandTips == null)
					_lockLandTips = new LockedLandTips(_isoWorld.sceneTipContainer);
				_lockLandTips.show(null, data);
			}
		*/
		}
		
		/**
		 * 获取锁定地图区域数据
		 * @param	grid
		 * @return
		 */
		private function getLockAreaData(gridX:int, gridZ:int, show:Boolean = true):AreaConfigData
		{
		/*	var lab:String = _isoWorld.mapInfo.areaInfo.getAreaKey(gridX, gridZ);
			if (_m_key == lab)
				return null;
			if (show)
				outArea();
			areMap = _isoWorld.mapInfo.areaInfo.areaMapDic[lab];
			if (!areMap)
			{
				if (_bindIsoTip)
					_bindIsoTip.close();
				if (_lockLandTips)
					_lockLandTips.close();
				if (_shopLockLandTips)
					_shopLockLandTips.close();
				return null;
			}
			if (areMap.maskBg)
			{
				areMap.maskBg.changeStatus(2);
				_isoWorld.crackLayer.showHighlightArea(true,areMap.maskBg.x,areMap.maskBg.y);
			}
			_m_key = lab;
			lockData = _controller.configModel.analysisArea.getAreaConfigDataById(lab);
			if (!lockData)
			{
				if (_bindIsoTip)
					_bindIsoTip.close();
				if (_lockLandTips)
					_lockLandTips.close();
				if (_shopLockLandTips)
					_shopLockLandTips.close();
				return null;
			}*/
			/*if(lockData.is_adjoin) lockData.is_neighbour = getNeighbourIsUnlock(areMap.keyX, areMap.keyZ);
			if(lockData.unlock_pre_xy) lockData.is_unlcok_pre = _isoWorld.mapInfo.areaInfo.unLockedAreDic[lockData.unlock_pre_xy];
			setUnlockRequireItemSize(lockData);
			
			if (!lockData.unlock_mission_data)
				lockData.u_mission_complete = true;
			else if (lockData.unlock_mission_data)
				lockData.u_mission_complete = _sceneModel.taskModel.checkTaskIsComplete(lockData.unlock_mission_id);*/
			
			return lockData;
		}
		
		
		/**
		 * 移开区域 
		 * 
		 */
		public function  outArea():void
		{
			if (!areMap)
				return;
			if (areMap.maskBg){
				areMap.maskBg.changeStatus();
				_isoWorld.crackLayer.showHighlightArea(false);
			}
			areMap = null;
			_m_key = null;
			clearCurrentTargetFocus(false);
		}
		
		/**
		 * 判断邻居是否解锁
		 * @param	x
		 * @param	y
		 * @return
		 */
		private function getNeighbourIsUnlock(x:int, y:int):Boolean
		{
			if (_isoWorld.mapInfo.areaInfo.unLockedAreDic[creatAreaKey(x - 1, y)])
				return true;
			if (_isoWorld.mapInfo.areaInfo.unLockedAreDic[creatAreaKey(x + 1, y)])
				return true;
			if (_isoWorld.mapInfo.areaInfo.unLockedAreDic[creatAreaKey(x, y - 1)])
				return true;
			if (_isoWorld.mapInfo.areaInfo.unLockedAreDic[creatAreaKey(x, y + 1)])
				return true;
			
			function creatAreaKey(x:int, y:int):String
			{
				return x + "_" + y;
			}
			return false;
		}
		
		/**
		 * 鼠标移出锁定区块
		 */
		private function mouseOutLockArea():void
		{
			if (!_m_key)
				return;
			_m_key = null;
			lockData = null;
			//移除TIP
		/*	if (_bindIsoTip)
				_bindIsoTip.close();
			if (_lockLandTips)
				_lockLandTips.close();
			if (_shopLockLandTips)
				_shopLockLandTips.close();*/
		}
		/***************************通用的一些调用方法 公开出去的************************/
	}
}